package day2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//测试文件复制(先对文件进行读取 在对文件进行写入)
public class TestCopy {
    public static void main(String[] args) {
        File in = new File("d://a//冒泡排序.txt");
        File out = new File("d://a//b//new冒泡排序.txt");
        if (in.exists()) {
            File parentOut = out.getParentFile();
            if (!parentOut.exists()) {
                parentOut.mkdirs();
            }
            //当前系统时间毫秒
            long start = System.currentTimeMillis();
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream(in);
                fos = new FileOutputStream(out);
                byte[] bs = new byte[1024];
                int len = 0;
                while ((len = fis.read(bs)) != -1) {
                    fos.write(bs, 0, len);
                }
                //在统计系统时间
                long end = System.currentTimeMillis();
                System.out.println("复制成功后所花费的时间: " + (end - start) + "ms");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null) fos.close();
                    if (fos != null) fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            System.out.println("要复制的文件不存在");
        }

    }

}
